package qf.sail.dao.impl;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import qf.sail.dao.OrderDao;
import qf.sail.entity.Account;
import qf.sail.entity.Order;
import qf.sail.entity.OrderItem;
import qf.sail.util.DBUtil;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    /**
     * 插入订单，返回插入订单的id号
     * @param order
     * @return
     */
    @Override
    public Long addOrder(Order order) {
        String sql = "INSERT INTO `order` (sn,status,account_id) VALUES (?,?,?)";
        Object[] params = {order.getSn(),order.getStatus(),order.getAccountId()};
        try {
            BigInteger id = queryRunner.insert(sql, new ScalarHandler<BigInteger>(), params);
            return id.longValue();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0L;
        }
    }

    /**
     * 更新订单金额
     * @param order
     * @return
     */
    @Override
    public int updateOrderMoney(Order order) {
        String sql = "update `order` set total_freight = ?,pay_money = ?,number = ? where id = ?";
        Object[] params = {order.getTotalFreight(),order.getPayMoney(),order.getNumber(),order.getId()};
        try {
            int update = queryRunner.update(sql,params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }

    /**
     * 根据订单Id获取订单
     * @param id
     * @return
     */
    @Override
    public Order getOrderById(long id) {
        String sql = "SELECT * FROM `order` WHERE id=?";
        Object[] params = {id};

        Order order = null;
        try {
            order = queryRunner.query(sql, DBUtil.getBeanHandler(Order.class),params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return order;
    }

    /**
     * 更改订单地址
     * @param address
     * @param orderId
     * @return
     */
    @Override
    public int updateOrderAddress(String address, long orderId) {
        String sql = "update `order` set address = ? where id = ?";
        Object[] params = {address,orderId};
        try {
            int update = queryRunner.update(sql,params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }

    @Override
    public Order getOrderBySn(String sn) {
        String sql = "SELECT * FROM `order` WHERE sn=?";
        Object[] params = {sn};

        Order order = null;
        try {
            order = queryRunner.query(sql, DBUtil.getBeanHandler(Order.class),params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return order;
    }

    @Override
    public int updateOrderStatus(String status,long orderId) {
        String sql = "update `order` set status = ? where id = ?";
        Object[] params = {status,orderId};
        try {
            int update = queryRunner.update(sql,params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Order> getOrderList(long accountId, int page) {
        String sql = "select * from `order` where account_id = ? order by create_time DESC limit ?,5";
        Object[] params = {accountId,(page-1)*5};
        List<Order> orders = null;
        try {
            orders = queryRunner.query(sql, DBUtil.getBeanListHandler(Order.class),params);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }

    @Override
    public int getCountById(long accountId) {
        String sql = "SELECT count(*) FROM `order` WHERE account_id=?";
        Object[] params = {accountId};
        int count = 0;
        try {
           long countL = (Long)queryRunner.query(sql,new ScalarHandler(),params);
           count = (int) countL;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
}
